package uy.com.csi.gestpro.util;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

public class Imagen {
	public static final double GRADES_90;

	static {
		GRADES_90 = 90.00;
	}

	public static void rotacionImagen(String fileOrigen, double grados) {
		try {
			// BufferedImage origen = ImageIO.read(new File(fileOrigen));

			byte[] rawData = getRawBytesFromFile(fileOrigen);
			ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(rawData));
			BufferedImage origen = ImageIO.read(iis);

			BufferedImage destinationImage;
			ImageTransform imTransform = new ImageTransform(origen.getHeight(), origen.getWidth());
			imTransform.rotate(grados);
			imTransform.findTranslation();
			AffineTransformOp ato = new AffineTransformOp(imTransform.getTransform(), AffineTransformOp.TYPE_BILINEAR);
			destinationImage = ato.createCompatibleDestImage(origen, origen.getColorModel());
			BufferedImage bf = ato.filter(origen, destinationImage);

			ImageIO.write(destinationImage, "jpg", new File(fileOrigen));
		} catch (IOException ex) {
			Logger.getLogger(Imagen.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void rotatePhoto(String fileOrigen) {
		try {
			File file = new File(fileOrigen);
			BufferedImage originalImage = ImageIO.read(file);

			// You could use Math.PI / 2, depends on your input.
			AffineTransform affineTransform = new AffineTransform();
			affineTransform.rotate(Math.toRadians(90), originalImage.getWidth() / 2, originalImage.getHeight() / 2);

			// Now lets make that transform an operation, and we use it.
			AffineTransformOp opRotated = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR);
			BufferedImage newImage = opRotated.filter(originalImage, null);

			// Save the image.
			File outputfile = new File(fileOrigen);
			ImageIO.write(newImage, "jpg", outputfile);
		} catch (IOException ex) {
			Logger.getLogger(Imagen.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	private static byte[] getRawBytesFromFile(String path) throws FileNotFoundException, IOException {

		byte[] image;
		File file = new File(path);
		image = new byte[(int) file.length()];

		FileInputStream fileInputStream = new FileInputStream(file);
		fileInputStream.read(image);

		return image;
	}

	public static String rotarImagen(String rutaImagen, double grados) {
		String rutaDestino = rutaImagen.substring(0, rutaImagen.lastIndexOf("\\") + 1);

		int posicion_img = rutaImagen.lastIndexOf("_") + 1;
		if (posicion_img < 0) {
			posicion_img = rutaImagen.lastIndexOf(".") + 1;
		}

		String nombreArchivo = rutaImagen.substring(rutaImagen.lastIndexOf("\\") + 1, posicion_img)
				+ new Fecha().getFecha(new Date(), Fecha.FORMATO_YYYYMMDDMMSS, Fecha.LOCALE_ES) + ".jpg";
		BufferedImage oldImage = null;
		try {
			oldImage = ImageIO.read(new FileInputStream(rutaImagen));
		} catch (IOException ex) {
			Logger.getLogger(Imagen.class.getName()).log(Level.SEVERE, null, ex);
		}
		BufferedImage newImage = new BufferedImage(oldImage.getHeight(), oldImage.getWidth(), oldImage.getType());
		Graphics2D graphics = (Graphics2D) newImage.getGraphics();
		graphics.rotate(Math.toRadians(90), newImage.getWidth() / 2, newImage.getHeight() / 2);
		graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);
		graphics.drawImage(oldImage, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null);
		try {
			ImageIO.write(newImage, "jpg", new File(rutaDestino + nombreArchivo));
		} catch (IOException ex) {
			Logger.getLogger(Imagen.class.getName()).log(Level.SEVERE, null, ex);
		}
		return nombreArchivo;
	}

	public void reducirPesoImagen(String input, String output, int porcentaje) {
		try {
			BufferedImage image = ImageUtils.loadImage(input);
			double pW = 0.6 * (porcentaje / 100.0) * image.getWidth();
			double pH = 0.6 * (porcentaje / 100.0) * image.getHeight();
			int newWidth = (int) (image.getWidth() - pW);
			int newHeight = (int) (image.getHeight() - pH);
			BufferedImage resizedImage = ImageUtils.resize(image, newWidth, newHeight);
			String format = (input.endsWith(".png")) ? "png" : "jpg";
			ImageIO.write(resizedImage, format, new File(output));
		} catch (IOException ex) {
			Logger.getLogger(Imagen.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
